<template>
  <div>
    <div>
      <RHTable :RHTable="table" ref="r_h_table"></RHTable>
    </div>
    <!--弹出框-->
    <div>
      <RHDialog :RHDialog="dialog" ref="dialog" :RHForm="form" :RHPre="pre"></RHDialog>
    </div>
  </div>
</template>
<script>
import RHTable from "@/common/RHTable/index.vue";
import RHDialog from "@/common/form/dialogForm.vue";
import * as service from "@/api/test";

export default {
  name: "user_index",
  data: function() {
    return {
      param: {},
      table: {
        url: this.$store.state.url.user + "/api/users/renHaiTest/getList",
        header: {
          show: true,
          btn: [{ value: "新增", click: this.show }]
        },
        hideColumn: true, //展示隐藏列
        columns: [
          { fieId: "id", title: "ID" },
          { fieId: "createId", title: "创建人id" },
          { fieId: "createName", title: "创建人姓名" },
          { fieId: "createTime", title: "创建时间" },
          { fieId: "updateId", title: "更新人id" },
          { fieId: "updateName", title: "更新人姓名" },
          { fieId: "updateTime", title: "修改时间" },
          { fieId: "remark", title: "备注" },
          { fieId: "startTime", title: "开始时间" },
          { fieId: "endTime", title: "结束时间" },
          { fieId: "type", title: "类型" },
          { fieId: "img", title: "图片", hide: true }
        ],
        columnsBtn: [
          { value: "修改", click: this.updatePre, size: "mini" },
          { value: "删除", click: this.delete, size: "mini" }
        ],
        // 分页
        paging: true
      },
      dialog: {
        visible: false,
        title: "添加模块",
        btn: [
          {
            value: "确定",
            type: "primary",
            click: this.submitForm
          }
        ]
      },
      form: [
        //隐藏
        { el: "hide", model: "id" },
        //自定义验证
        {
          el: "input",
          label: "编号",
          prop: [
            {
              validator: async (rule, value, callback) => {
                const id = this.$refs.dialog.getForm().getForm("id");
                let res = await service.checkCode({
                  code: value,
                  id: id
                });
                if (res.entity > 0) {
                  callback(new Error("编号不能重复！"));
                } else {
                  callback();
                }
              },
              trigger: "blur"
            },
            { required: true, message: "请输入编号", trigger: "blur" }
          ],
          model: "code"
        },
        {
          el: "select",
          label: "类型",
          prop: { required: true },
          model: "type",
          multiple: false, //是否多选
          candidate: {
            label: "name",
            value: "id",
            url:
              this.$store.state.url.user + "/api/users/renHaiTest/getSelectList"
          }
        },
        {
          el: "input",
          label: "顺序号",
          type: "number",
          prop: [{ required: true, max: 999, min: 0,  message: "请输入顺序号", type: "number" }],
          model: "seq"
        },
        {
          el: "file",
          label: "上传文件",
          prop: [{ required: true, type: "array", message: "请选择文件" }],
          model: "img",
          fileData: {
            imgBtn: {
              show: true,
              value: "选择图片"
            }
          }
        },
        {
          el: "date",
          dataType: "datetimerange",
          label: "开始结束时间",
          prop: [{ required: true, message: "请选择开始时间" }],
          startModel: "startTime",
          endModule: "endTime"
        }
      ],
      pre: {
        status: false,
        url: this.$store.state.url.user + "/api/users/renHaiTest/getById",
        param: {
          id: null
        }
      }
    };
  },
  methods: {
    show() {
      this.dialog.visible = true;
    },
    resetForm() {
      this.$refs.r_h_form.resetForm();
    },
    submitForm() {
      this.$refs.dialog.getForm().RHFormValidate((valid, form) => {
        if (valid) {
          form.img = JSON.stringify(form.img);
          service.saveUpdate(form).then(data => {
            this.$util.message(data);
            //刷新表格
            this.$refs.r_h_table.refresh();
            //关闭弹窗
            this.dialog.visible = false;
          });
        }
      });
    },
    updatePre(param) {
      this.dialog.visible = true;
      this.pre.status = true;
      this.pre.param.id = param.id;
    },
    delete(param) {
      const that = this;
      console.log(this.$refs.r_h_form);
      that.$util.messageBox({ msg: "确定删除？" }, function() {
        service.del({ userId: param.userId }).then(function(data) {
          that.$util.message(data);
          //刷新表格
          that.$refs.r_h_table.refresh();
        });
      });
    }
  },
  components: {
    RHTable,
    RHDialog
  }
};
</script>

<style scoped></style>
